package com.visikard.asynctask;

import android.os.Handler;

import com.visikard.application.UIMessage;
import com.visikard.business.AudienceBUS;
import com.visikard.response.BaseResponse;
import com.visikard.response.SendAudienceToWalletResponse;

public class SendAudienceToWalletTask extends BaseTask {

	public static final int POS_UID = 0;
	public static final int POS_ID_CAMPAIGN_SPECIAL = 1;
	public static final int POS_LIST_FK_USER = 2;
	public static final int POS_LIST_FK_KARD = 3;
	public static final int POS_IS_BROADCAST = 4;

	public SendAudienceToWalletTask(Handler handler) {
		super(handler);
	}

	public SendAudienceToWalletTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doBackgroundWork(Object... params) {
		// Get params
		long uid = (Long) params[POS_UID];
		long idCampaignSpecial = (Long) params[POS_ID_CAMPAIGN_SPECIAL];
		String listFkUser = (String) params[POS_LIST_FK_USER];
		String listFkKard = (String) params[POS_LIST_FK_KARD];
		int isBroadcast = (Integer) params[POS_IS_BROADCAST];

		// validation:

		// call service
		AudienceBUS busObj = new AudienceBUS();
		return busObj.sendAudienceToWallet(uid, idCampaignSpecial, listFkUser, listFkKard, isBroadcast);
	}

	@Override
	protected boolean handleResult(Object result) {
		SendAudienceToWalletResponse response = (SendAudienceToWalletResponse) result;
		if (response != null) {
			switch (response.getErrorCode()) {
				case BaseResponse.SUCCESS:
					if (response.getMessage().equals("SUCCESS"))
						mUIHandler.sendEmptyMessage(UIMessage.MSG_SEND_TO_WALLET_SUCCESS);
					else
						mUIHandler.sendEmptyMessage(UIMessage.MSG_SEND_TO_WALLET_FAILED);
					return true;
				case BaseResponse.NETWORK_ERROR:
					mUIHandler.sendEmptyMessage(UIMessage.MSG_SEND_TO_WALLET_FAILED);
					return false;
				default: // unknown error
					mUIHandler.sendEmptyMessage(UIMessage.MSG_SEND_TO_WALLET_FAILED);
					return false;
			}
		}
		return false;
	}

}
